package cn.woochen.mvvmframe.activity

import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import cn.woochen.comlib.util.FragmentManagerHelper
import cn.woochen.comlib.util.ScreenUtil
import cn.woochen.comlib.util.StatusBarUtil
import cn.woochen.mvvmframe.R
import cn.woochen.mvvmframe.config.BaseActivity
import cn.woochen.mvvmframe.databinding.ActivityTestBinding
import cn.woochen.mvvmframe.test.BlankFragment
import cn.woochen.mvvmframe.test.BlankFragment2
import cn.woochen.mvvmframe.util.logcom
import cn.woochen.mvvmframe.viewmodels.EmptyViewModel


/**
 *类描述：测试页面
 *@author woochen
 *@time 2021/7/29 10:02
 */
class TestActivity : BaseActivity<ActivityTestBinding, EmptyViewModel>() {

    private val mFragmentManagerHelper1 by lazy {
        FragmentManagerHelper(supportFragmentManager,R.id.fl_content1)
    }
    private val mFragmentManagerHelper2 by lazy {
        FragmentManagerHelper(supportFragmentManager,R.id.fl_content2)
    }

    private var isMyLayout = true

    override fun setContentView(): Int =  R.layout.activity_test

    override fun initData() {
        mFragmentManagerHelper1.addFragment(BlankFragment.newInstance(),"blank1")
        mFragmentManagerHelper2.addFragment(BlankFragment2.newInstance(),"blank2")
        mBinding.refreshLayout.setOnRefreshListener {
            it.finishRefresh()
            logcom("refresh..finish")
            switchLayout()
        }
    }

    private val screenHeight by lazy {
        (ScreenUtil.getHeight(mContext) + StatusBarUtil.getStatusBarHeight(mContext)).toFloat()
    }

    private val animator1 by lazy {
        ObjectAnimator.ofFloat(mBinding.flContent2, "translationY", 0f, screenHeight)
    }

    private val animator2 by lazy {
        ObjectAnimator.ofFloat(mBinding.flContent1, "translationY", 0f, screenHeight)
    }

    private val animator11 by lazy {
        ObjectAnimator.ofFloat(mBinding.flContent2, "translationY", -screenHeight,0f )
    }

    private val animator22 by lazy {
        ObjectAnimator.ofFloat(mBinding.flContent1, "translationY", -screenHeight,0f )
    }

    private fun switchLayout() {
        if (isMyLayout){
            animator1.start()
            animator22.start()
        }else{
            animator2.start()
            animator11.start()
        }
        isMyLayout = !isMyLayout
    }

    companion object{
        fun start(context: Context){
            val intent = Intent(context,TestActivity::class.java)
            context.startActivity(intent)
        }
    }


}